/**
 * 初始化一颗zTree - 终端群组
 *
 * data:
 * {
 *   name: xxx
 *   pid: xxx
 *   showIcon: true
 *   check: true
 *   chkStyle: radio
 *   checkType: {"Y":"ps", "N":"ps"}
 *   radioType: all
 *
 *   editenable: true
 *   showRemoveBtn:
 *   showRenameBtn:
 *   dragIsCopy:
 *   dragIsMove:
 *   dragPrev:
 *   dragNext:
 *   borderMax:
 *   borderMin:
 *
 *   expand: false
 *
 *   beforeClick:
 *   beforeRemove:
 *   beforeDrag:
 *   beforeDrop:
 *   onclick: xxx
 *   onDrop:
 *
 *   init:初始化方法
 * }
 *
 */
function getClientZtree(divId, param){

    //默认配置
    var settings = {
        name: "gname",
        pid: "pid"
    }

    if(param){
        settings = param;
    }

    //调用自定义ajax
    myajax({
        url: "/client/group",
        async: param.async != undefined ? param.async : true,
        success: function(data){
            //生成树
            createZtree(divId, data, settings);
        }
    });
}

/**
 * 获得群组信息 - 根据分控
 */
function getWebconClientZtree(divId, param){
    //默认配置
    var settings = {
        name: "gname",
        pid: "pid"
    }

    if(param){
        settings = param;
    }

    //调用自定义ajax
    myajax({
        url: "/client/groupWebcon",
        async: param.async != undefined ? param.async : true,
        success: function(data){
            //生成树
            createZtree(divId, data, settings);
        }
    });
}

/**
 * 获得ztree对象
 */
function getZtree(divId){
    return $.fn.zTree.getZTreeObj(divId ? divId : "ztree_div");
}

/**
 * 创建一个Ztree
 */
function createZtree(divid, data, param){

    var setting = {
        edit: {
            enable: param.editenable != undefined ? param.editenable : false,
            showRemoveBtn: param.showRemoveBtn != undefined ? param.showRemoveBtn : false,
            showRenameBtn: param.showRenameBtn != undefined ? param.showRenameBtn : false,
            drag:{
                isCopy: param.dragIsCopy != undefined ? param.dragIsCopy : false,
                isMove: param.dragIsMove != undefined ? param.dragIsMove : false,
                prev: param.dragPrev != undefined ? param.dragPrev : false,
                next: param.dragNext != undefined ? param.dragNext : false,
                borderMax: param.borderMax ? param.borderMax : 20,
                borderMin: param.borderMin ? param.borderMin : -10
            }
        },
        data: {
            key: {
                name: param.name ? param.name : "gname"
            },
            simpleData: {
                enable: true,
                pIdKey: param.pid ? param.pid : "pid"
            }
        },
        view: {
            showIcon: param.showIcon != undefined ? param.showIcon : true
        },
        check: {
            enable: param.check != undefined ? param.check : false,
            chkStyle: param.chkStyle ? param.chkStyle: "checkbox",
            chkboxType: param.checkType ? param.checkType : { "Y": "ps", "N": "ps" },
            radioType: param.radioType ? param.radioType : "level"
        },
        callback: {
            beforeClick: param.beforeClick ? param.beforeClick : null,
            beforeRemove: param.beforeRemove ? param.beforeRemove : null,
            beforeDrag: param.beforeDrag ? param.beforeDrag : null,
            beforeDrop: param.beforeDrop ? param.beforeDrop : null,
            onClick: param.onClick ? param.onClick : null,
            onDrop: param.onDrop ? param.onDrop : null
        }
    };

    var zNodes = data;

    //初始化zTree
    var ztreeObject = $.fn.zTree.init($("#" + divid), setting, zNodes);
    ztreeObject.expandAll(param.expand != undefined ? param.expand : true);//全部展开

    //如果设置了初始化方法，就执行初始化方法
    if(param.init){
        param.init(ztreeObject, data);
    }

    return ztreeObject;
}